﻿/*ajax示例代码*/

$.ajax({
    type: "post",
    url: "test.aspx?command=save",
    data: "", //JSON2.stringify({ ItemsCategory: value, Division: divi, year: _year }),
    //contentType: "application/json; charset=utf-8",
    //dataType: "json",
    success: function (data) {

    },
    error: function (err) { alert(err.responseText); }
});
$.ajax({
    async: false,
    type: "post",
    url: "BudgetApplyList.aspx?command=getApplyInfo",
    data: "year=xx",
    dataType: "json",//接收类型
    contentType: "application/x-www-form-urlencoded", //请求类型
    success: function (data) {

    },
    error: function (err) {
        alert("ajax错误：" + err.responseText);
    }
});

/*监听一个iframe的ajax行为
第一步：写一个callback，监听函数
function onAjaxSend(method, url, data) {  
}  

第二步：设置iframe的onload属性为onload="ajaxSend(this.contentWindow.XMLHttpRequest, onAjaxSend);" 
不支持IE6
*/
function ajaxSend(objectOfXMLHttpRequest, callback) {
    // http://stackoverflow.com/questions/3596583/javascript-detect-an-ajax-event  
    if (!callback) {
        return;
    }

    var s_ajaxListener = new Object();
    s_ajaxListener.tempOpen = objectOfXMLHttpRequest.prototype.open;
    s_ajaxListener.tempSend = objectOfXMLHttpRequest.prototype.send;
    s_ajaxListener.callback = function () {
        // this.method :the ajax method used  
        // this.url :the url of the requested script (including query string, if any) (urlencoded)  
        // this.data :the data sent, if any ex: foo=bar&a=b (urlencoded)  
        callback(this.method, this.url, this.data);
    }

    objectOfXMLHttpRequest.prototype.open = function (a, b) {
        if (!a) var a = '';
        if (!b) var b = '';
        s_ajaxListener.tempOpen.apply(this, arguments);
        s_ajaxListener.method = a;
        s_ajaxListener.url = b;
        if (a.toLowerCase() == 'get') {
            s_ajaxListener.data = b.split('?');
            s_ajaxListener.data = s_ajaxListener.data[1];
        }
    }

    objectOfXMLHttpRequest.prototype.send = function (a, b) {
        if (!a) var a = '';
        if (!b) var b = '';
        s_ajaxListener.tempSend.apply(this, arguments);
        if (s_ajaxListener.method.toLowerCase() == 'post') {
            s_ajaxListener.data = a;
        }
        s_ajaxListener.callback();
    }
}